<template>
  <el-dialog
    :close-on-click-modal="false"
    title="其他合同"
    :visible.sync="show"
    width="1000px"
    center
  >
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="120px">
      <div class="com-form-title">基础1信息</div>
      <el-row>
        <el-col :span="8">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="form.projectName" :disabled="disableForm || optType !== 'add'">
              <i slot="suffix" class="el-input__icon el-icon-circle-plus-outline" @click="optType === 'add' ? showTargetProjectSetListModal = true : null" />
            </el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="8">
          <el-form-item label="合同编号" prop="contractNumber">
            <el-input v-model="form.contractNumber" :disabled="disableForm" />
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item label="合同名称" prop="contractName">
            <el-input v-model="form.contractName" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="合同金额" prop="contractAmount">
            <el-input v-model="form.contractAmount" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="合同类型" prop="contractType">
            <el-select v-model="form.contractType" :disabled="disableForm" style="width: 100%;" placeholder="请选择合同类型">
              <el-option v-for="item in otherContractInfoContractType" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="甲方名称" prop="employerName">
            <el-input v-model="form.employerName" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="甲方代表" prop="employerRepresent">
            <el-input v-model="form.employerRepresent" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="甲方电话" prop="employerPhone">
            <el-input v-model="form.employerPhone" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="乙方名称" prop="employeeName">
            <!-- <el-input v-model="form.employeeName" :disabled="disableForm" /> -->
            <el-input v-model="form.employeeName" :disabled="disableForm || optType !== 'add'">
              <i slot="suffix" class="el-input__icon el-icon-circle-plus-outline" @click="optType === 'add' ? showTeamListModal = true : null" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="乙方代表" prop="employeeRepresent">
            <el-input v-model="form.employeeRepresent" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="乙方电话" prop="employeePhone">
            <el-input v-model="form.employeePhone" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="合同开始时间" prop="contractStartTime">
            <el-date-picker v-model="form.contractStartTime" style="width:100%" type="date" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="合同结束时间" prop="contractEndTime">
            <el-date-picker v-model="form.contractEndTime" style="width:100%" type="date" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>

      <div class="com-form-title">附件</div>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
    <team-list :show-modal.sync="showTeamListModal" @getSelectedRows="getSelectedRowsTeam" />
    <project-list :show-modal.sync="showTargetProjectSetListModal" @getSelectedRows="getSelectedProjectRows" />
  </el-dialog>
</template>

<script>
import { addOtherContractInfo, updateOtherContractInfo, getOtherContractInfoDetail } from '@/api/contractManage'
import { getAreaList } from '@/api/common'
import { otherContractInfoContractType, workflowStatus } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
import TeamList from '@/components/TreeModal/team'
import ProjectList from '@/components/TreeModal/project'
export default {
  name: 'AddOtherContractInfo',
  components: {
    fileUploader,
    TeamList,
    ProjectList
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showTeamListModal: false,
      showFileModal: false,
      selectRow: null,
      targetList: [],
      otherContractInfoContractType,
      workflowStatus,
      showTargetProjectSetListModal: false,
      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        projectName: '',
        // contractNumber: '',
        contractName: '',
        contractAmount: '',
        contractType: '',
        employerName: '',
        employerRepresent: '',
        employerPhone: '',
        employeeName: '',
        employeeRepresent: '',
        employeePhone: '',
        contractStartTime: '',
        contractEndTime: '',
        remark: '',
        addWorkflowId: '',
        status: '',
        founderName: '',
        createTime: ''
      },
      fileList: [
      ],
      rules: {
        contractName: [
          { required: true, message: '请输入合同名称', trigger: 'change' }
        ],
        projectName: [
          { required: true, message: '请输入项目名称', trigger: 'change' }
        ],
        employerName: [
          { required: true, message: '请输入甲方名称', trigger: 'change' }
        ],
        employeeName: [
          { required: true, message: '请输选择乙方名称', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
        this.fileList = []
      }
    }
  },
  watch: {
    showModal(val) {
      if (val && this.optType !== 'add') {
        this.getDocDetail(this.data.id)
        this.$nextTick(() => {
          Object.keys(this.form).forEach(key => {
            const source = this.data[key]
            if (typeof source === 'boolean') {
              this.form[key] = source ? 1 : 0
            } else {
              this.form[key] = source
            }
          })
        })
      } else if (!val) {
        this.fileList = []
        this.$refs.form.resetFields()
      }
    }
  },
  mounted() {
    this.getRegionData()
  },
  methods: {
    getSelectedRowsTeam(obj) {
      this.form.contractNumber = obj.bidNumber
      this.form.contractName = obj.bidName
      this.form.contractAmount = obj.boBidPrice
      this.form.employeeName = obj.boBidUnit
    },
    getSelectedProjectRows(obj) {
      this.form.projectName = obj.row.projName
    },
    provinceChange(e) {
      const selectedData = this.provinceData.filter(val => val.name === e)[0]
      if (selectedData) {
        this.form.city = ''
        const params = {
          parentCode: selectedData.code,
          areaType: 2
        }
        getAreaList(params).then(res => {
          this.cityData = res.data || []
        })
      }
    },
    getDocDetail(id) {
      getOtherContractInfoDetail({ otherContractInfoId: id }).then(res => {
        const { lstAttachment } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then(res => {
        this.provinceData = res.data || []
      })
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            otherContractInfo: {
              ...this.form,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstAttachment: this.fileList
          }
          const fn = this.optType === 'add' ? addOtherContractInfo : updateOtherContractInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>
